package com.tiheima.controller;

import com.tiheima.model.entity.Dept;
import com.tiheima.model.result.Result;
import com.tiheima.service.DeptService;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/depts")
public class DeptController {
    private final DeptService deptService;

    public DeptController(DeptService deptService) {
        this.deptService = deptService;
    }

    @GetMapping
    public Result<List<Dept>> depts() {
        List<Dept> deptList = deptService.depts();
        return Result.success(deptList);
    }

    @DeleteMapping
    public Result<?> deleteDeptById(Integer id) {
        deptService.deleteDept(id);
        return Result.success();
    }

    @PostMapping
    public Result<?> add(@RequestBody Dept dept) {
        deptService.saveDept(dept);
        return Result.success();
    }

    @GetMapping("/{id}")
    public Result<Dept> getDeptById(@PathVariable("id") Integer id) {
        Dept dept = deptService.getDeptById(id);
        return Result.success(dept);
    }

    @PutMapping
    public Result<?> update(@RequestBody Dept dept) {
        deptService.updateDeptById(dept);
        return Result.success();
    }
}
